<?php


namespace app\admin\controller;


use app\common\controller\Adminbase;
use think\Db;

class Querorder2 extends Adminbase
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $filter = input('filter');
            $where[] = ['is_del', '=', 0];
            $where[] = ['order_type', '=', 2];
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['id'])) {
                    $where[] = ['id', '=', $filter['id']];
                }
                if (isset($filter['name'])) {
                    $where[] = ['name', 'like', '%' . $filter['name'] . '%'];
                }
                if (isset($filter['status'])) {
                    $where[] = ['status', '=', $filter['status']];
                }

                if (isset($filter['is_js'])) {
                    $where[] = ['is_js', '=', $filter['is_js']];
                }
                if (isset($filter['is_j'])) {
                    $where[] = ['is_j', '=', $filter['is_j']];
                }
                if (isset($filter['qw_type'])) {
                    $where[] = ['qw_type', '=', $filter['qw_type']];
                }

                if (isset($filter['create_time'])) {
                    $create_time = explode(' - ', $filter['create_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['create_time', 'between', [$create_time[0], $create_time[1]]];
                }
            }
            $_list = Db::name('query_order')
                ->where($where)->page($page, $limit)
                ->order('create_time desc')
                ->select();
            $total = Db::name('query_order')
                ->where($where)
                ->count();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                $_list[$k]['ji_data'] = date('Y-m-d H:i:s', $v['ji_data']);
                $_list[$k]['start_time'] = date('Y-m-d H:i:s', $v['start_time']);
                if ($v['status_time']) {
                    $_list[$k]['status_time'] = date('Y-m-d H:i:s', $v['status_time']);
                } else {
                    $_list[$k]['status_time'] = '--------';
                }
            }
            $result = array("code" => 0, "count" => $total, "data" => $_list);
            return json($result);
        }
        return $this->fetch();
    }

    //删除
    public function del()
    {
        $ids = $this->request->param('ids/a', null);
        try {
            foreach ($ids as $id) {
                Db::name('query_order')->where('id', $id)->update([
                    'is_del' => 1
                ]);
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('删除成功！');
    }

    public function edit()
    {
        $id = $this->request->param('id/d', 0);
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if ($data['assess_file']) {
                $update_data = array(
                    'id' => $data['id'],
                    'assess_file' => $data['assess_file'],
                    'status' => 1,
                    'status_time' => time(),
                    'query_order_desc'=>$data['query_order_desc']
                );
                Db::name('query_order')->update($update_data);
                $content=$data['name'].'汽配查询订单已评估完成，可通过订单动态查询相关详细信息！';
                if ($data['status']==0){
                    sendMsg(1, '汽配查询订单状态更新', $content, $data['member_id']);
                }
            }
            $this->success('操作成功！');
        }
        $data = Db::name('query_order')->find($id);
        $query_order_file = Db::name('query_order_file')->where([
            'query_order_id' => $id
        ])->select();
        $data['create_time'] = date('Y-m-d H:i:s', $data['create_time']);
        $data['ji_data'] = date('Y-m-d H:i:s', $data['ji_data']);
        $data['start_time'] = date('Y-m-d H:i:s', $data['start_time']);
        if ($data['status_time']) {
            $data['status_time'] = date('Y-m-d H:i:s', $data['status_time']);
        } else {
            $data['status_time'] = '待评估';
        }
        $this->assign([
            'query_order_file' => $query_order_file,
            'data' => $data,
        ]);
//        attachment
        return $this->fetch('');
    }

    public function editfee()
    {
        $id = $this->request->param('id/d', 0);
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            $update_data = array(
                'id' => $data['id'],
                'js_money' => $data['js_money'],
                'is_js'=>1,
                'js_time'=>time()
            );
            $stmt = Db::name('query_order')->update($update_data);
            $query_order=Db::name('query_order')->find($id);

            addMoney($query_order['member_id'],$data['js_money'],2,$id,3);
            $content=$query_order['name'].'估价消耗余额'.$data['js_money'].'元';
            sendMsg(2, '估价消耗余额', $content, $query_order['member_id']);

            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
            return $data;
        }
        $data = Db::name('query_order')->find($id);
        $this->assign('data', $data);
        return $this->fetch();
    }
}